package com.socialuni.uniapp.sdk.utils;

import com.socialuni.uniapp.sdk.constant.UniPlatformType;
import com.socialuni.uniapp.sdk.constant.WxUrl;
import com.socialuni.uniapp.sdk.exception.UniSdkException;
import com.socialuni.uniapp.sdk.model.UniProviderLoginQO;
import com.socialuni.uniapp.sdk.model.UniUnionIdRO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Map;
import java.util.Objects;

@Component
public class WxProviderUtil {
    private static String wxMpId;
    private static String wxMpSecret;

    @Value("${uniapp.wx-mp-id}")
    public void setWxMpId(String wxMpId) {
        WxProviderUtil.wxMpId = wxMpId;
    }

    @Value("${uniapp.wx-mp-secret}")
    public void setWxMpSecret(String wxMpSecret) {
        WxProviderUtil.wxMpSecret = wxMpSecret;
    }

    public static UniUnionIdRO getWxUnionIdRO(UniProviderLoginQO unionIdData) throws UniSdkException {
        String platform = unionIdData.getPlatform();
        String code = unionIdData.getCode();

        if (UniPlatformType.mp.equals(platform)) {
            String url = WxProviderUtil.getUnionIdUrl(platform, code);
            ResponseEntity<UniUnionIdRO> responseEntity = RestUtil.restTemplate().getForEntity(url, UniUnionIdRO.class);
            // 微信需要单独解析
            return WxProviderUtil.decodeUnionId(unionIdData, Objects.requireNonNull(responseEntity.getBody()));

        } else if (UniPlatformType.app.equals(platform)) {
            // 微信需要单独解析
            UniUnionIdRO uniUnionIdRO = new UniUnionIdRO();
            uniUnionIdRO.setOpenid(unionIdData.getOpenId());
            uniUnionIdRO.setUnionid(unionIdData.getUnionId());
            return uniUnionIdRO;
        }

        throw new UniSdkException(UniPlatformType.notSupportTypeErrorMsg);
    }

    private static String getUnionIdUrl(String platform, String code) throws UniSdkException {
        if (UniPlatformType.mp.equals(platform)) {
            return MessageFormat.format(WxUrl.wx_mp_unionId_url, wxMpId, wxMpSecret, code);
        }
        throw new UniSdkException(UniPlatformType.notSupportTypeErrorMsg);
    }

    private static UniUnionIdRO decodeUnionId(UniProviderLoginQO loginData, UniUnionIdRO uniUnionIdRO) {
        //只有unionid为空才解析，什么时候会为空，需要确认
        if (StringUtils.isEmpty(uniUnionIdRO.getUnionid())) {
            String enData = loginData.getEncryptedData();
            String enIv = loginData.getIv();
            //都不为空才解析unionid
            if (StringUtils.isNotEmpty(enData) && StringUtils.isNotEmpty(enIv)) {
                String userInfoJson = WxDecode.decrypt(enData, uniUnionIdRO.getSession_key(), enIv);
                //不为空
                if (StringUtils.isNotEmpty(userInfoJson)) {
                    try {
                        Map map = JsonUtils.objectMapper.readValue(userInfoJson, Map.class);
                        String enUnionId = (String) map.get("unionId");
                        uniUnionIdRO.setUnionid(enUnionId);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return uniUnionIdRO;
    }
}
